
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include "disp_manager.h"

static int fd_fb;
static struct fb_var_screeninfo var;	/* Current var */
static int screen_size;
static unsigned char *fb_base;
static unsigned int line_width;
static unsigned int pixel_width;





//var.bits_per_pixel代表一个 像素用多少位来表示
static int DeviceInit_frame(void)
{
	fd_fb = open("/dev/fb0", O_RDWR);
	if (fd_fb < 0)
	{
		printf("can't open /dev/fb0\n");
		return -1;
	}
	if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
	{
		printf("can't get var\n");
		return -1;
	}

//一行多宽个字节
	line_width	= var.xres * var.bits_per_pixel / 8;
//每像素宽多少个字节
	pixel_width = var.bits_per_pixel / 8;
//屏幕大小
	screen_size = var.xres * var.yres * var.bits_per_pixel / 8;

	//获取framebuffer的基地址
	fb_base = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
	if (fb_base == (unsigned char *)-1)
	{
		printf("can't mmap\n");
		return -1;
	}

	return 0;




}
int DeviceExit_frame(void)
{


	munmap(fb_base, screen_size);

	close(fd_fb);
	return 0;
	


}



//返回LCD的framebuffer，上层APP可以直接操作LCD
static int getbuff_frame(DispBuff_p dispBuff)
{
	dispBuff->xres =  var.xres;
	dispBuff->yres = var.yres;
	dispBuff->b_p = var.bits_per_pixel;//默认16位深度
	dispBuff->buff = fb_base;
	return 0;

}


//刷到硬件
static int  FLushRegion_frame(Region_p Regionp, DispBuff disbuff)
{




	return 0;


}


 
 struct display_operation getframebuffer_operation = {
 
 	.name = "fb",
	.DeviceInit =DeviceInit_frame,
	.DeviceExit =DeviceExit_frame,
	.getbuff = getbuff_frame,
	.FLushRegion =FLushRegion_frame,


};



void FramebufferInit(void)
{

//注册getframebuffer_operation
  RegsiterDisplay(&getframebuffer_operation);
//wedinit



}



























